import { Module } from "vuex";

import { copyTemplate } from "@/api/decoration/decoration";
import { Message } from "element-ui";
const module: Module<any, any> = {
  namespaced: true,
  state: {
    // 可以更改的临时模板id
    templateId: "",
    // 最终保存的模板id
    saveTemplateId: "",
    // 自定义页面名称
    pageName: "自定义页面",
    // 自定义页面属性
    activePage: null,
    // 当前页面操作组件下标
    activeComIndex: null,
    // 设为首页id
    homePageId: null,
    // 管理页面管理tab栏 customize:自定义 userCenter:用户中心 bussiness:营销
    activePageType: "customize",
    // 复制链接
    copyLink: "/pages/index/index?page=home",
    isUsercenterCompontents: false,
    userCenterType: "",
    firstStatus: false,
    // 左侧标签页面
    activeTab: 0,
    /** 是否底部导航添加链接 */
    getFrom: "",
    /** 商品专区id */
    saleMode: "",
    isCustom: false,

    /** 直播内容暂存 */
    liveItem: [],
  },
  mutations: {
    // 实时更新首页id
    SET_HOME_PAGE_ID(state, pageId) {
      state.homePageId = pageId;
    },
    // 实时更新获取到链接来源
    SET_LINK_FORM(state, from) {
      state.getFrom = from;
    },
    // 更改临时模板id
    SET_TEMPLATEID(state, templateId) {
      state.templateId = templateId;
    },
    // 判断用户是不是第一次进入编辑页面
    SET_FIRSTSTATUS(state, data) {
      state.firstStatus = data;
    },
    // 更改最终保存的模板id
    SET_SAVETEMPLATEID(state, templateId) {
      state.saveTemplateId = templateId;
    },
    // 使用装修模板时的自定义页面名称
    SET_PAGE_NAME(state, pageName) {
      state.pageName = pageName;
    },
    // 设置当前页面预览对象
    SET_ACTIVEPAGE(state, activePage) {
      if (["usercenter", "businessSuper"].includes(activePage)) {
        state.isUsercenterCompontents = true;
        state.userCenterType = activePage;
        state.isCustom = false;
        state.liveItem = [];
        state.activePage = [];
      } else {
        state.activePage = activePage;
        state.saleMode = activePage.modelId;
        state.isUsercenterCompontents = false;
        state.userCenterType = "";
        state.liveItem = [];

        if (activePage.modelId) {
          state.isCustom = true;
        }
        if (activePage.name === "直播页面") {
          state.isCustom = true;
          state.liveItem = activePage;
        }
      }
    },
    SET_ISUSERCENTER(state, data) {
      state.isUsercenterCompontents = false;
      state.userCenterType = "";
      state.activeTab = data;
    },
    SET_ACTIVEPAGETYPE(state, { type, link }) {
      state.activePageType = type;
      state.copyLink = link;
    },
    SET_ACTIVECOMINDEX(state, data) {
      state.activeComIndex = data;
    },
  },
  actions: {
    /**
     * 更改临时模板id
     * @param templateId
     */
    updateTemplateId({ commit }, templateId) {
      copyTemplate({ id: templateId }).then(res => {
        if (res.code === 200) {
          commit("SET_TEMPLATEID", res.data.id);
          commit("SET_ACTIVEPAGE", null);
        } else {
          Message({
            message: res.msg,
          });
        }
      });
    },
    /**
     * 更改最终保存的模板id
     * @param templateId
     */
    updateSaveTemplateId({ commit }, templateId) {
      commit("SET_SAVETEMPLATEID", templateId);
    },
    /** 更改自定义页面名称 */
    updatePageName({ commit }, pageName) {
      commit("SET_PAGE_NAME", pageName);
    },
  },
};

export default module;
